<?php
//namespace student\controller\welcome;
defined('BASEPATH') OR exit('No direct script access allowed');

class Captcha extends CI_Controller {


	public function __construct()
	{
		parent::__construct();
		//$this->load->library('session');
		session_start();
	}


	public function index()
	{
		$str = $this->random(4);
		$_SESSION['code'] = $str;
		$this->createImg($str);
	}
    
	public function index2()
	{
		/**
		 * 字母+数字的验证码生成
		 */
		
		//1.创建黑色画布
		$image = imagecreatetruecolor(100, 30);
		 
		//2.为画布定义(背景)颜色
		$bgcolor = imagecolorallocate($image, 255, 255, 255);
		 
		//3.填充颜色
		imagefill($image, 0, 0, $bgcolor);
		 
		// 4.设置验证码内容
		 
		//4.1 定义验证码的内容
		$content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
		 
		//4.1 创建一个变量存储产生的验证码数据，便于用户提交核对
		$captcha = "";
		for ($i = 0; $i < 4; $i++) {
		    // 字体大小
		    $fontsize = 10;
		    // 字体颜色
		    $fontcolor = imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
		    // 设置字体内容
		    $fontcontent = substr($content, mt_rand(0, strlen($content)), 1);
		    $captcha .= $fontcontent;
		    // 显示的坐标
		    $x = ($i * 100 / 4) + mt_rand(5, 10);
		    $y = mt_rand(5, 10);
		    // 填充内容到画布中
		    imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
		}

		//$captcha_array = array('captcha'=>$captcha);
        //$this->session->set_userdata('zl','abcd');
        $this->session->set_userdata('zl','zhanglei666777888');
        /*print $this->session->userdata('zl');
        return 0;*/

		//$_SESSION['captcha'] = $captcha;
		//$_SESSION['name'] = 'zhangsan';
		

		//4.3 设置背景干扰元素
		for ($$i = 0; $i < 200; $i++) {
		    $pointcolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
		    imagesetpixel($image, mt_rand(1, 99), mt_rand(1, 29), $pointcolor);
		}
		 
		//4.4 设置干扰线
		for ($i = 0; $i < 3; $i++) {
		    $linecolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
		    imageline($image, mt_rand(1, 99), mt_rand(1, 29), mt_rand(1, 99), mt_rand(1, 29), $linecolor);
		}
		 
		//5.向浏览器输出图片头信息
		header('content-type:image/png');
		 
		//6.输出图片到浏览器
		imagepng($image);
		 
		//7.销毁图片
		imagedestroy($image);
	}


   
	public function createImg($str)
	{
		/*$str = $this->random(4);

		$_SESSION['captcha'] = $str;*/
		//验证码图片的宽度
		$width  = 50;
		      		 
		//验证码图片的高度
		$height = 25;     
		 
		//声明需要创建的图层的图片格式
		@header("Content-Type:image/png");
		 
		//创建一个图层
		$im = imagecreate($width, $height);
		 
		//背景色
		$back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
		 
		//模糊点颜色
		$pix  = imagecolorallocate($im, 187, 230, 247);
		 
		//字体色
		$font = imagecolorallocate($im, 41, 163, 238);
		 
		//绘模糊作用的点
		mt_srand();
		for ($i = 0; $i < 1000; $i++) {
		    imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix);
		}
		 
		//输出字符
		imagestring($im, 5, 7, 5, $str, $font);
		 
		//输出矩形
		imagerectangle($im, 0, 0, $width -1, $height -1, $font);
		 
		//输出图片
		imagepng($im);
		 
		imagedestroy($im);
		 
		//$str = md5($str);
		 
		//选择 cookie
		//SetCookie("verification", $str, time() + 7200, "/");
		 
		//选择 Session

	}

	public function random($len){
		$srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm";
	    mt_srand();
	    $strs = "";
	    for ($i = 0; $i < $len; $i++) {
	        $strs .= $srcstr[mt_rand(0, 30)];
	    }
	    return $strs;
	}
}
